package com.example.knowledgeget1.GraphDBController;

import java.util.List;
import java.util.Map;

import com.example.knowledgeget1.Common.ResponseResult;
import com.example.knowledgeget1.GraphDBCommon.Service.NodeService;
import com.example.knowledgeget1.KnowtCommon.Response.NodeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.example.knowledgeget1.GraphDBCommon.Entity.Node;


@RestController
@RequestMapping("/nodes")
public class NodeController {
    @Autowired
    private NodeService nodeService;

    //单个节点导入
    @PostMapping("/saveNode")
    public ResponseResult saveNode(@RequestBody Node node) {
        node = nodeService.saveNode(node);
        return ResponseResult.success(node);
    }

    // 更新结点
    @PostMapping("/updateNode")
    public ResponseResult updateNode(@RequestBody Map<String, String> request) {
        Node newNode = new Node();
        newNode.setName(request.get("name"));
        newNode.setIntroduce(request.get("introduce"));
        newNode.setFunction(request.get("function"));
        newNode.setGrammar(request.get("grammar"));
        newNode.setCharacteristic(request.get("characteristic"));
        newNode.setExample(request.get("example"));
        newNode.setExercise(request.get("exercise"));
        newNode.setRecommendation(request.get("recommendation"));
        newNode = nodeService.saveNode(newNode);
        return ResponseResult.success(newNode);
    }

    //存储pre关系
    @PostMapping("/savePre/{node1}/{node2}")
    public ResponseEntity<Node> savePre(@PathVariable String node1, @PathVariable String node2) {
        if(node1 == null || node2 == null) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        //如果结点不存在
        if(nodeService.findNodeByName(node1)==null||nodeService.findNodeByName(node2)==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        if(nodeService.existsPreRelationship(node1,node2)){
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }else{
            nodeService.addPreRelationshipById(node1,node2);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    //删除pre关系
    @DeleteMapping("deletePre/{name1}/{name2}")
    public ResponseEntity<Node> deletePre(@PathVariable String name1, @PathVariable String name2) {
        if(name1 == null || name2 == null) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        if(nodeService.existsPreRelationship(name1,name2)){
            nodeService.deletePreRelationshipByName(name1,name2);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }else{
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    //存储general关系
    @PostMapping("/saveGeneral/{node1}/{node2}")
    public ResponseEntity<Node> saveGeneral(@PathVariable String node1, @PathVariable String node2) {
        if(node1 == null || node2 == null) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        //如果结点不存在
        if(nodeService.findNodeByName(node1)==null||nodeService.findNodeByName(node2)==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        if(nodeService.existsGeneralRelationship(node1,node2)){
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }else{
            nodeService.addGeneralRelationshipById(node1,node2);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    //删除general关系
    @DeleteMapping("deleteGeneral/{name1}/{name2}")
    public ResponseEntity<Node> deleteGeneral(@PathVariable String name1, @PathVariable String name2) {
        if(name1 == null || name2 == null) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        if(nodeService.existsGeneralRelationship(name1,name2)){
            nodeService.deleteGeneralRelationshipByName(name1,name2);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }else{
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 通过Name删除结点
    @DeleteMapping("deleteNode/{name}")
    public ResponseResult deleteNodeByName(@PathVariable String name) {
        nodeService.deleteByName(name);
        return ResponseResult.success();
    }

    // 获取所有知识点
    @GetMapping("/getAllNodes")
    public ResponseResult getAllNodes() {
        List<Node> nodes = nodeService.findAllNodes();
        return ResponseResult.success(nodes);
    }

    // 根据名称获取知识点及其前置后置知识点
    @GetMapping("/details/{name}")
    public ResponseEntity<?> getNodeDetailsByName(@PathVariable String name) {
        NodeResponse nodeResponse = nodeService.findNodeWithPostKnowledgesByName(name);
        if (nodeResponse.getName() == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(nodeResponse);
    }

}
